package tools.bowtie;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import tools.gff.gffLine;
import tools.gff.gffLine_startComparator;

public class bowtieUtils {

	private final static String sep="\t";
	
	public static void main(String[] args) throws Exception{
		if(args.length>0){
			if(args[0].equals("overlapGff")&&args.length>2){
				overlapGff(args[1],args[2]);
			}else if(args[0].equals("topDesc")&&args.length==5){
				//top(args[1],Integer.parseInt(args[2]),Integer.parseInt(args[3]),Integer.parseInt(args[4]),false);
			}else if(args[0].equals("tabulatedTopOnly")&&args.length==2){
				//other method
			}else{
				System.err.println(printHelp());
				System.exit(616);
			}
		}else{
			System.err.println(printHelp());
			System.exit(616);
		}
	}
	private static String printHelp(){
		String help="Usage: bowtieUtils <cmd> <input>\n";
		help+="where <cmd> is:\n";
		help+="overlapGff - Overlap to the exons in a GFF3-file. Reports the parents in the info line. No regard to strand\n";
		help+="\t<input> = <bowtieFile> <GFF3 file>\n";
		
		
		
		return help;
	}
	
	
	public static void overlapGff(String bowtieFile, String GffFile)throws Exception{
		HashMap<String, ArrayList<gffLine>> targets= new HashMap<String, ArrayList<gffLine>>();
		BufferedReader in= new BufferedReader(new FileReader(GffFile));
		gffLine gff;
		bowtieLine_small bwl;
		//read and store the gff file
		for(String s=in.readLine();s!=null;s=in.readLine()){
			try{
				gff= new gffLine(s);
				if(gff.getFeature().equals("exon")){
					if(!targets.containsKey(gff.getChr())){
						targets.put(gff.getChr(), new ArrayList<gffLine>());
					}
					targets.get(gff.getChr()).add(gff);
				}
			}catch(Exception e){
				e.printStackTrace(System.err);
				System.err.println("Strange gff line:\n"+s);
			}
		}
		//sort and prepare the gffLines
		for (ArrayList<gffLine> chr : targets.values()) {
//			System.err.println(chr.size());
//			System.err.println(chr.get(0).toString());
			Collections.sort(chr, new gffLine_startComparator());
//			System.err.println(chr.get(0).toString());
		}
		//read the bowtie file and check for overlap
		in= new BufferedReader(new FileReader(bowtieFile));
		
		for(String s=in.readLine();s!=null;s=in.readLine()){
			try{
				bwl= new bowtieLine_small(s);
				if(targets.containsKey(bwl.getTname())){
					//check for overlap
					for (gffLine gffLine : targets.get(bwl.getTname())) {
						if(gffLine.getStart()>bwl.getEnd()){
							break;
						}else if(gffLine.overlap(bwl.getStart(), bwl.getEnd())){
							if(gffLine.hasParent()){
								for (String parent : gffLine.getParents()) {
									System.out.println(bwl.qname+sep+parent);
								}
							}else{
								System.err.println("GffLine without parent:\n"+gffLine.toString());
							}
						}
					}
				}else{
					//No overlap
				}
			}catch (Exception e) {
				e.printStackTrace(System.err);
				System.err.println("Strange bowtie line:\n"+s);
			}
		}
	}
}
